﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Data.OleDb;
using System.Data;

namespace Web_HengliSeed.BLL
{
    public class NewsInfo
    {
        /// <summary>
        /// 获取一条分类数据
        /// </summary>
        /// <param name="strClassID"></param>
        /// <returns></returns>
        public static OleDbDataReader GetClassMes(string strClassID)
        {
            try
            {
                OleDbParameter[] parm = { new OleDbParameter("@T_NewsClassID", strClassID) };
                return ClassFile.AccessHelper.ExecuteReader(ClassFile.AccessHelper.ConnectionString, " SELECT * FROM T_NewsClass WHERE T_NewsClassID=@T_NewsClassID ", parm);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 获取所有的分类信息
        /// </summary>
        /// <returns></returns>
        public static OleDbDataReader GetClassMesAll(string lan)
        {
            try
            {
                return ClassFile.AccessHelper.ExecuteReader(ClassFile.AccessHelper.ConnectionString, " SELECT * FROM T_NewsClass WHERE T_Language='" + lan + "'", null);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 编辑类别数据
        /// </summary>
        /// <param name="ClassID"></param>
        /// <param name="strClassName"></param>
        /// <param name="strKey"></param>
        /// <param name="strDes"></param>
        /// <returns></returns>
        public static bool EditClassMes(string ClassID, string strClassName, string strKey, string strDes, string lan)
        {
            try
            {
                OleDbParameter[] parm;
                string strSql = " INSERT INTO  T_NewsClass (T_NewsClassName,T_NewsClassKey,T_NewsClassDes,T_Language) VALUES (@T_NewsClassName,@T_NewsClassKey,@T_NewsClassDes,@T_Language) ";
                parm = new OleDbParameter[]{new OleDbParameter("@T_NewsClassName", strClassName),
                                                           new OleDbParameter("@T_NewsClassKey", strKey),
                                                           new OleDbParameter("@T_NewsClassDes", strDes),
                                                           new OleDbParameter("@T_Language",lan),
                                                           new OleDbParameter("@T_NewsClassID",string.IsNullOrEmpty(ClassID) ? 0 : int.Parse(ClassID))
                                                           
                                                           };

                if (!string.IsNullOrEmpty(ClassID))
                {
                    strSql = "  UPDATE T_NewsClass SET T_NewsClassName=@T_NewsClassName,T_NewsClassKey=@T_NewsClassKey,T_NewsClassDes=@T_NewsClassDes  WHERE T_NewsClassID=@T_NewsClassID ";
                    parm = new OleDbParameter[]{new OleDbParameter("@T_NewsClassName", strClassName),
                                                           new OleDbParameter("@T_NewsClassKey", strKey),
                                                           new OleDbParameter("@T_NewsClassDes", strDes),
                                                           new OleDbParameter("@T_NewsClassID",string.IsNullOrEmpty(ClassID) ? 0 : int.Parse(ClassID))
                                                           };
                }
                 
                return (ClassFile.AccessHelper.ExecuteNonQuery(ClassFile.AccessHelper.ConnectionString, strSql, parm) > 0) ? true : false;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 删除类别信息
        /// </summary>
        /// <param name="strClassID"></param>
        /// <returns></returns>
        public static bool DelClassMes(string strClassID)
        {
            try
            {
                OleDbParameter[] parm = { new OleDbParameter("@T_NewsClassID", strClassID) };
                bool ReClass = (ClassFile.AccessHelper.ExecuteNonQuery(ClassFile.AccessHelper.ConnectionString, " DELETE FROM T_NewsClass WHERE T_NewsClassID=@T_NewsClassID ", parm) > 0) ? true : false;
                DelNewsClassInfo(strClassID);
                return ReClass;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        ///获取一条资讯信息
        /// </summary>
        /// <param name="strProID"></param>
        /// <returns></returns>
        public static OleDbDataReader GetNewsInfo(string strProID)
        {
            try
            {
                OleDbParameter[] parm = { new OleDbParameter("@T_NewsID", strProID) };
                return ClassFile.AccessHelper.ExecuteReader(ClassFile.AccessHelper.ConnectionString, " SELECT * FROM T_NewsInfo WHERE T_NewsID=@T_NewsID ", parm);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        ///获取最新资讯信息
        /// </summary>
        /// <param name="strProID"></param>
        /// <returns></returns>
        public static OleDbDataReader GetTopNewsInfo(string lan)
        {
            try
            {
                return ClassFile.AccessHelper.ExecuteReader(ClassFile.AccessHelper.ConnectionString, " SELECT TOP 5 T_NewsID,T_NewsTitle,T_DateTime FROM T_NewsInfo WHERE T_Language='" + lan + "' ORDER BY T_NewsID ", null);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 删除指定ID的资讯
        /// </summary>
        /// <param name="strProClass"></param>
        /// <returns></returns>
        public static bool DelNewsInfo(string strProID)
        {
            try
            {
                string strSql = string.Format(" DELETE FROM T_NewsInfo WHERE T_NewsID in({0}) ", strProID);
                return (ClassFile.AccessHelper.ExecuteNonQuery(ClassFile.AccessHelper.ConnectionString, strSql, null) > 0) ? true : false;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 删除指定类别下的所有的产品
        /// </summary>
        /// <param name="strProClass"></param>
        /// <returns></returns>
        public static bool DelNewsClassInfo(string strProClassID)
        {
            try
            {
                string strSql = string.Format(" DELETE FROM T_NewsInfo WHERE T_NewsClassID ={0} ", strProClassID);
                return (ClassFile.AccessHelper.ExecuteNonQuery(ClassFile.AccessHelper.ConnectionString, strSql, null) > 0) ? true : false;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 编辑资讯数据（更新）
        /// </summary>
        /// <param name="ClassID"></param>
        /// <param name="strClassName"></param>
        /// <param name="strKey"></param>
        /// <param name="strDes"></param>
        /// <returns></returns>
        public static bool EditNewsMes(string strT_ProductID, string strT_ProductName, string strT_ProClassID, string strT_ProductNote, string lan)
        {
            try
            {
                string strSql = " INSERT INTO  T_NewsInfo (T_NewsTitle,T_NewsClassID,T_NewsNote,T_DateTime,T_Language) VALUES (@T_NewsTitle,@T_NewsClassID,@T_NewsNote,@T_DateTime,@T_Language) ";
                if (!string.IsNullOrEmpty(strT_ProductID))
                {
                    strSql = "UPDATE  T_NewsInfo SET T_NewsTitle=@T_NewsTitle,T_NewsClassID=@T_NewsClassID,T_NewsNote=@T_NewsNote,T_DateTime=@T_DateTime WHERE  T_NewsID=@T_NewsID ";
                }
                OleDbParameter[] parm = { new OleDbParameter("@T_NewsTitle", strT_ProductName),
                                                           new OleDbParameter("@T_NewsClassID", strT_ProClassID),
                                                           new OleDbParameter("@T_NewsNote", strT_ProductNote),
                                                           new OleDbParameter("@T_DateTime", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")),
                                                           new OleDbParameter("@T_NewsID", strT_ProductID),
                                                            new OleDbParameter("@T_Language", lan)};
                return (ClassFile.AccessHelper.ExecuteNonQuery(ClassFile.AccessHelper.ConnectionString, strSql, parm) > 0) ? true : false;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 分页查询资讯信息
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageCount"></param>
        /// <returns></returns>
        public static DataTable GetAllNewsInfo(int PageIndex, int PageSize, out int PageCount, out int RecordCount, string strWhere, string lan)
        {
            try
            {
                //if (string.IsNullOrEmpty(strWhere))
                //    strWhere += " where T_Language='" + lan + "'";
                //else
                //    strWhere += " and T_Language='" + lan + "'";

                string strSql = " SELECT P.T_NewsID,P.T_NewsClassID,P.T_NewsTitle,P.T_DateTime,C.T_NewsClassName,C.T_NewsClassKey,C.T_NewsClassDes,P.T_NewsNote FROM T_NewsInfo P  " +
                                " LEFT JOIN T_NewsClass C ON P.T_NewsClassID=C.T_NewsClassID WHERE C.T_Language='" + lan + "' ";
                string strShow = " T_NewsID,T_NewsTitle,T_NewsClassName,T_DateTime,T_NewsClassKey,T_NewsClassDes,T_NewsNote ";
                return ClassFile.AccessHelper.ExecutePager(PageIndex, PageSize, "T_NewsID", strShow, strSql, strWhere, " T_DateTime DESC ", out PageCount, out RecordCount);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

    }
}
